package com.hqyj.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hqyj.dao.AfterServiceMapper;
import com.hqyj.pojo.AfterService;
import com.hqyj.pojo.Customer;
import com.hqyj.pojo.DataMessage;
import com.hqyj.service.AfterServices;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AfterServicesImpl implements AfterServices {
    @Resource
    AfterServiceMapper afterServiceMapper;

    @Override
    public DataMessage list(AfterService a) {
        DataMessage data =new DataMessage();
        //1.定义分页参数对象：startPage(页码，每页显示条数)
        PageHelper.startPage(a.getPage(),a.getRow());
        //自定义查询
        List<AfterService> list =afterServiceMapper.queryByAfter(a);
        //创建分页结果集对象
        PageInfo<AfterService> pageResult =new PageInfo<AfterService>(list);
        //判断分页结果集是否有对象
        if(pageResult!=null){
            Map<String,Object> map =new HashMap<>();
            //当前页集合
            map.put("list",pageResult.getList());
            //总页数
            map.put("totalPage",pageResult.getPages());
            //总条数
            map.put("total",pageResult.getTotal());
            data.setSuccess(true);
            data.setData(map);
        }else {
            data.setSuccess(false);
            data.setInfo("查询失败");
        }
        return data;
    }

    @Override
    public DataMessage update(AfterService a) {
        DataMessage data =new DataMessage();
        int num =afterServiceMapper.updateByPrimaryKey(a);
        if(num>0){
            data.setSuccess(true);
            data.setInfo("修改成功");
        }else {
            data.setSuccess(false);
            data.setInfo("修改失败");
        }
        return data;
    }

    @Override
    public DataMessage add(AfterService a) {
        DataMessage data = new DataMessage();
        int num = afterServiceMapper.insert(a);
        if(num>0){
            data.setSuccess(true);
            data.setInfo("修改成功");
        }else {
            data.setSuccess(false);
            data.setInfo("修改失败");
        }
        return data;
    }
}
